package hongcaosp.app.android.user.member;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;

import java.util.List;

import hongcaosp.app.android.R;

public class MemberLayout extends LinearLayout implements MemberItemHolder.MemberItemSelectListener {
    private List<IMemberDate> dates;
    private MemberItemHolder selectItem;
    private MemberItemHolder.MemberItemSelectListener memberItemSelectListener;

    public void setMemberItemSelectListener(MemberItemHolder.MemberItemSelectListener memberItemSelectListener) {
        this.memberItemSelectListener = memberItemSelectListener;
    }

    public MemberLayout(Context context) {
        super(context);
    }

    public MemberLayout(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public MemberLayout(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public void initDates(List<IMemberDate> dates) {
        this.dates = dates;
        removeAllViews();
        if (this.dates != null) {
            for (IMemberDate member :  dates) {
                MemberItemHolder memberItemHolder = new MemberItemHolder(
                        LayoutInflater.from(getContext()).inflate(R.layout.item_member_date,this,false));
                memberItemHolder.setData(member);
                memberItemHolder.setItemSelectListener(this);
                addView(memberItemHolder.itemView);
            }
        }
    }

    @Override
    public void onItemSelected(MemberItemHolder memberItemHolder) {
        if (selectItem != null) {
            selectItem.setSelected(false);
        }
        selectItem = memberItemHolder;
        if (memberItemSelectListener != null) {
            memberItemSelectListener.onItemSelected(memberItemHolder);
        }
    }
}
